// 尝试从缓存中读取数据
const localData = localStorage.getItem('pz_v3pz')
// 如果localData存在，用localData.Menu，否则用初始数据
const state = localData ? localData.Menu : {
    isCollapse: false,
    selectMenu: [],
    routerList: [],
    menuActive: '1-1'
}

const mutations = {
    collapseMenu(state) {
        state.isCollapse = !state.isCollapse
    },
    addMenu(state, payload) {
        if (state.selectMenu.findIndex(item => item.path === payload.path) === -1) {
            state.selectMenu.push(payload)
        }
    },
    removeMenu(state, payload) {
        const index = state.selectMenu.findIndex(item => item.path === payload.path)
        if (index !== -1) {
            state.selectMenu.splice(index, 1)
        }
    },
    dynamicMenu(state, payload) {
        // 通过glob导入文件，生成组件
        const modules = import.meta.glob('../views/**/**/*.vue')
        console.log(modules, 'modules')
        function routerSet(router) {
            // router：动态路由数据
            router.forEach(route => {
                // 判断没有子菜单，则拼接路由数据
                if (!route.children) {
                    const url = `../views${route.meta.path}/index.vue`
                    // 根据url获取到对应的vue组件赋值到动态路由
                    route.component = modules[url]
                }
                else {
                    // 有子菜单则递归
                    routerSet(route.children)
                }
            })
        }
        routerSet(payload)
        // 拿到完整的路由数据
        state.routerList = payload
    },
    updateSelectMenu(state, payload) {
        state.menuActive = payload
    }
}

export default {
    state,
    mutations
}